<?php
class Articles_add extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		// 登录校验
		$this->common->check_login_admin ();
		$this->load->helper ( array (
				'form',
				'url' 
		) );
	}
	
	// 公告文章详情页
	public function index() {
		$data ['left'] = $this->load->view ( 'admin/left', '', TRUE );
		$data ['menu'] = $this->load->view ( 'admin/menu', '', TRUE );
		$this->load->view ( 'admin/articles_add', $data );
	}
	
	// 新增公告内容
	public function add() {
		if (empty ( $_POST ['title'] ) || empty ( $_POST ['content'] )) {
			exit ( $this->common->alert_msg_goback ( '发布失败，内容不全。' ) );
		}
		
		$path = $this->do_upload ( "picture" );
		$_POST ['picture'] = $path ['file_name'];
		
		$this->load->model ( 'notice_model' );
		$result = $this->notice_model->notice_insert ( $_SESSION ['admin'] ['id'] );
		
		if ($result) {
			exit ( $this->common->alert_msg_url ( '发布成功', site_url ( 'admin/articles_list' ) ) );
		} else {
			exit ( $this->common->alert_msg_goback ( '发布失败' ) );
		}
	}
	
	/* 保存图片 */
	public function do_upload($name) {
		$config ['upload_path'] = './uploads/';
		$config ['allowed_types'] = 'gif|jpg|png';
		$config ['max_size'] = 10000;
		$config ['max_width'] = 102400;
		$config ['max_height'] = 76800;
		
		$this->load->library ( 'upload', $config );
		
		if (! $this->upload->do_upload ( $name )) {
			$path = $this->upload->display_errors ();
		} else {
			$path = $this->upload->data ();
		}
		
		return $path;
	}
}